package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.adapter.SubmitFileListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.ProjectProgressOriginal.ProjectProgressSuper;
import com.isunland.managesystem.entity.SubmitFileOriginal;
import com.isunland.managesystem.entity.SubmitFileOriginal.SubmitFileContent;
import java.util.ArrayList;
import java.util.HashMap;

public class SubmitFileListFragment extends BaseListFragment {
  public static final String EXTRA_CONTENT =
      "com.isunland.managesystem.ui.SubmitFileListFragment.EXTRA_CONTENT";

  private static final int DETAIL_FILE = 0;
  private ProjectProgressSuper superContent;
  private CurrentUser mUser;
  private ArrayList<SubmitFileContent> mlist;
  private SubmitFileListAdapter mAdapter;

  @Override public void onCreate(Bundle savedInstanceState) {
    mUser = CurrentUser.newInstance(getActivity());
    superContent = (ProjectProgressSuper) getArguments().getSerializable(EXTRA_CONTENT);
    super.onCreate(savedInstanceState);
  }

  @Override public String getUrl() {
    return ApiConst.ULR_SUBMIT_FILE_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("memberCode", mUser.getMemberCode());
    params.put("stageId", superContent.getId());
    params.put("page", "1");
    params.put("rows", "1000");
    return params;
  }

  @Override public void resolveResponse(String json) {
    SubmitFileOriginal original = new Gson().fromJson(json, SubmitFileOriginal.class);
    ArrayList<SubmitFileContent> rows = (ArrayList<SubmitFileContent>) original.getRows();
    if (mlist == null) {
      mlist = new ArrayList<SubmitFileOriginal.SubmitFileContent>();
    }
    mlist.clear();
    mlist.addAll(rows);
    setAdapter(mlist);
  }

  private void setAdapter(ArrayList<SubmitFileContent> mlist) {
    if (mAdapter == null) {
      mAdapter = new SubmitFileListAdapter(getActivity(), mlist);
    }
    setListAdapter(mAdapter);
    ((SubmitFileListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    SubmitFileContent content = mlist.get(position - 1);
    Intent i = new Intent(getActivity(), DetailSubmitFileActivity.class);
    i.putExtra(DetailSubmitFileFragment.EXTRA_CONTENT, content);
    startActivityForResult(i, DETAIL_FILE);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == DETAIL_FILE) {
      volleyPost();
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  public static SubmitFileListFragment newInstance(ProjectProgressSuper item) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, item);
    SubmitFileListFragment fragment = new SubmitFileListFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
